import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_CAPACITY = 10;
    public MyStack(){
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public void push(int val){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[usedSize] = val;
        usedSize++;
    }
    public boolean isFull(){
        return usedSize == elem.length;
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }
    public int pop(){
        if(isEmpty()){
        throw new EmptyException();
        }
        int oldVal = elem[usedSize-1];
        usedSize--;
        return oldVal;
    }

    public int peek(){
        if(isEmpty()){
            throw new EmptyException();
        }
        return elem[usedSize-1];
    }


    //括号匹配
    //只要是左括号就入栈 右括号就出栈
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i< s.length(); i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch =='{' || ch == '['){
                stack.push(ch);

            }else{
                if(stack.empty()){
                    return false;
                }
                char top = stack.peek();
                if(ch == ')'&& top == '(' ||ch == '}' && top == '{'||ch ==']'&& top =='['){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }

    //逆波兰表达式  栈中放数字 遇到操作度 拿出第一个作为右边的操作数 第二个作为左边的操作数
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s: tokens){
            if(!isOperation(s)){
                //数字
                stack.push(Integer.parseInt(s));

            }else{
                //运算符
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s){
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }

        }
        return stack.pop();
    }
    private boolean isOperation(String s){
        if(s.equals("+") ||s.equals("-")  ||s.equals("*") ||s.equals("/") ){
            return true;
        }
        return false;
    }

    //栈的压入、弹出序列
    //输入两个整数序列，第一个序列表示栈的压入顺序
    // 请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i< pushV.length; i++){
            stack.push(pushV[i]);
            while(!stack.empty() && j < popV.length && stack.peek() ==popV[j] ){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }


    //最小栈
    //设计一个支持 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈。
    class MinStack {

        private Stack<Integer> stack;
        private Stack<Integer> minStack;

        public MinStack() {
            stack = new Stack<>();
            minStack = new Stack<>();
        }

        public void push(int val) {
            stack.push(val);
            if(minStack.empty()) {
                minStack.push(val);
            }else {
                if(val <= minStack.peek()) {
                    minStack.push(val);
                }
            }
        }

        public void pop() {
            if(!stack.empty()) {
                int ret = stack.pop();
                if(minStack.peek() == ret) {
                    minStack.pop();
                }
            }
        }
        //获取正常栈顶元素
        public int top() {
            if(stack.empty()) {
                return -1;
            }
            return stack.peek();
        }
        //获取最小栈顶元素
        public int getMin() {
            if(minStack.empty()) {
                return -1;
            }
            return minStack.peek();
        }
    }

    //用队列实现栈




}
